﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BL.Abstract;
using DAL.Hibernate.Entities;
using DAL.Hibernate.Abstract;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Linq;
using NHibernate.Engine;

namespace BL.Managers
{
    public class DBManager<T> : IDBManager<T>
    {
        #region Fields

        private readonly ISession _session;

        #endregion

        #region Constructors

        public DBManager(ISession session)
        {
            _session = session;
        }

        #endregion

        #region IDBManager Members

        public object Create(T obj)
        {

            _session.Save(obj);
            _session.Flush();
            return obj;

        }

        public T Update(T obj)
        {
            _session.Update(obj);
            _session.Flush();
            return obj;
        }

        public void Delete(T obj)
        {
            _session.Delete(obj);
            _session.Flush();
        }

        public IList<T> GetAll()
        {
            return _session.Query<T>().ToList();
        }

        public IQueryable<T> Query()
        {
            return _session.Query<T>();
        }

        public void Flush()
        {
            _session.Flush();
        }

        #endregion

        
    }
}
